package code09集合.Set;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class Test06_自定义排序规则 {

    public static void main(String[] args) {
        System.out.println("*************************Comparable排序*************************");
        User user = new User("admin", 20);
        User user2 = new User("jack", 20);
        User user3 = new User("alice", 21);
        User user4 = new User("lucy", 22);
        User user5 = new User("tom", 21);
        User user6 = new User("alex", 20);
        Set<User> users = new TreeSet<>(Arrays.asList(user, user2, user3, user4, user5, user6));
        for (User u : users) {
            System.out.println(u);
        }

        System.out.println("*************************Comparator排序*************************");
        // 定义一个TreeSet,将比较器交给它
        Set<Person> persons = new TreeSet<>(new PersonComparator());
        persons.add(new Person("admin", 20));
        persons.add(new Person("alice", 21));
        persons.add(new Person("jack", 20));
        persons.add(new Person("lucy", 22));
        persons.add(new Person("tom", 20));
        persons.add(new Person("alex", 21));
        persons.add(new Person("lily", 22));
        for (Person person : persons) {
            System.out.println(person);
        }
    }

}